package com.zycfc.zsf.boot.office.pdf.generator;

import java.io.*;
import java.util.jar.*;
import com.zycfc.zsf.boot.util.text.*;
import java.net.*;
import java.util.*;

public class ResourceUtil
{
    private static final String INVALID_PATH = "\u8def\u5f84\u975e\u6cd5\uff1a";
    private static final String JAR_FILE_PATH_PRE = "jar:";
    private static final String FILE_PATH_PRE = "file:";
    private static final String FILE_PARAM = "\u5165\u53c2\u7684file\u5bf9\u8c61";
    private static final String FILES_PARAM = "\u5165\u53c2\u7684file\u5bf9\u8c61\u96c6\u5408";
    private static final String JAR_FILE_SUFFIX = ".jar";
    private static final String FILE_SEPARATOR = "/";
    private static final String JAR_SEPARATOR = "!/";
    private static final String POINT_SEPARATOR = ".";
    
    protected static List<File> getFiles(final String path, final List<String> fileSuffixList, final boolean givinAbsolutePath) {
        final List<File> fileList = new ArrayList<File>();
        return findFiles(givinAbsolutePath ? path : getAbsolutePath(path), fileSuffixList, fileList);
    }
    
    protected static List<URL> getFileUrls(final String path, final List<String> fileSuffixList, final boolean givinAbsolutePath, final String matchPath) throws IOException {
        List<URL> urls = new ArrayList<URL>();
        final String absolutePath = givinAbsolutePath ? path : getAbsolutePath(path);
        if (absolutePath.contains(".jar")) {
            final String jarPath = absolutePath.substring(0, absolutePath.lastIndexOf(".jar") + 4);
            return getJarFileUrls(jarPath, matchPath, fileSuffixList, givinAbsolutePath);
        }
        final List<File> fileList = new ArrayList<File>();
        final List<File> resultFileList = findFiles(absolutePath, fileSuffixList, fileList);
        urls = filesToUrls(resultFileList, false);
        return urls;
    }
    
    protected static List<URL> getJarFileUrls(String jarPath, final String path, final List<String> fileSuffixList, final boolean givinAbsolutePath) throws IOException {
        final List<URL> urls = new ArrayList<URL>();
        final boolean hasSuffixFlag = fileSuffixList != null && fileSuffixList.size() > 0;
        jarPath = (givinAbsolutePath ? jarPath : getAbsolutePath(jarPath));
        final String jarUrlPath = jarPath.contains("file:") ? jarPath : ("file:" + jarPath);
        final JarFile jarFile = new JarFile(new File(jarUrlPath.substring(5)));
        final String matchPath = path.startsWith("/") ? path.substring(1) : path;
        final Enumeration<JarEntry> jarEntryEnumeration = jarFile.entries();
        while (jarEntryEnumeration.hasMoreElements()) {
            final JarEntry jarEntry = jarEntryEnumeration.nextElement();
            final String jarInnerPath = jarEntry.getName();
            if (!jarEntry.isDirectory()) {
                if (jarInnerPath.indexOf(matchPath) < 0) {
                    continue;
                }
                if (hasSuffixFlag && (!hasSuffixFlag || !containTheFile(fileSuffixList, jarInnerPath))) {
                    continue;
                }
                final String urlPath = new StringBuffer("jar:").append(jarUrlPath).append("!/").append(jarInnerPath).toString();
                urls.add(new URL(urlPath));
            }
        }
        return urls;
    }
    
    private static String getAbsolutePath(final String path) {
        final URL url = ResourceUtil.class.getResource(path);
        if (url == null) {
            throw new IllegalArgumentException(new StringBuffer("\u8def\u5f84\u975e\u6cd5\uff1a").append(path).toString());
        }
        return url.getPath();
    }
    
    private static List<File> findFiles(final String path, final List<String> fileSuffixList, List<File> fileList) {
        if (fileList == null) {
            fileList = new ArrayList<File>();
        }
        final File file = new File(path);
        final boolean hasSuffixFlag = fileSuffixList != null && fileSuffixList.size() > 0;
        if (file.isDirectory()) {
            final File[] listFiles;
            final File[] files = listFiles = file.listFiles();
            for (final File item : listFiles) {
                if (item.isDirectory()) {
                    findFiles(item.getPath(), fileSuffixList, fileList);
                }
                else if (hasSuffixFlag && containTheFile(fileSuffixList, item.getPath())) {
                    fileList.add(item);
                }
                else if (!hasSuffixFlag) {
                    fileList.add(item);
                }
            }
        }
        else if (hasSuffixFlag && containTheFile(fileSuffixList, file.getPath())) {
            fileList.add(file);
        }
        else if (!hasSuffixFlag) {
            fileList.add(file);
        }
        return fileList;
    }
    
    private static boolean containTheFile(final List<String> fileSuffixList, final String filePath) {
        return fileSuffixList.contains(filePath.substring(filePath.lastIndexOf(".")));
    }
    
    protected static URL fileToUrl(final File file, final boolean fileInJar) throws MalformedURLException {
        CheckUtil.notNull((Object)file, "\u5165\u53c2\u7684file\u5bf9\u8c61");
        final String filePath = file.getPath().contains("file:") ? file.getPath() : ("file:" + file.getPath());
        return fileInJar ? new URL("jar:" + filePath) : new URL(filePath);
    }
    
    protected static List<URL> filesToUrls(final List<File> files, final boolean fileInJar) throws MalformedURLException {
        CheckUtil.notEmpty((Object)files, "\u5165\u53c2\u7684file\u5bf9\u8c61\u96c6\u5408");
        final List<URL> urls = new ArrayList<URL>();
        for (final File file : files) {
            final String filePath = file.getPath().contains("file:") ? file.getPath() : ("file:" + file.getPath());
            urls.add(fileInJar ? new URL("jar:" + filePath) : new URL(filePath));
        }
        return urls;
    }
}
